package net.lab1024.sa.admin.module.sast.task.service;

import jakarta.annotation.Resource;
import net.lab1024.sa.admin.constant.SastConst;
import net.lab1024.sa.admin.module.business.category.domain.entity.CategoryEntity;
import net.lab1024.sa.admin.module.sast.project.domain.ProjectEntity;
import net.lab1024.sa.admin.module.sast.project.service.ProjectService;
import net.lab1024.sa.admin.module.sast.task.dao.TaskDao;
import net.lab1024.sa.admin.module.sast.task.domain.TaskEntity;
import net.lab1024.sa.admin.module.sast.task.domain.TaskForm;
import net.lab1024.sa.admin.module.system.employee.service.EmployeeService;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class TaskService {


    @Resource
    private TaskDao taskDao;
    @Resource
    private ProjectService projectService;

    @Resource
    private EmployeeService employeeService;

    public ResponseDTO<String> create(TaskForm taskForm,Long userId){
        ResponseDTO<String> res=checkTask(taskForm);
        if(!res.getOk()){
            return res;
        }
        TaskEntity taskEntity=SmartBeanUtil.copy(taskForm, TaskEntity.class);
        taskEntity.setStatus(0);
        taskEntity.setType(SastConst.TASK_TYPE_ANALYSIS);//改成在前端传
        String userUid=employeeService.getById(userId).getEmployeeUid();
        taskEntity.setSubmitterLogin(userUid);
        taskDao.create(taskEntity);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> checkTask(TaskForm taskForm){
        String componentUuid=taskForm.getComponentUuid();
        Optional<ProjectEntity> optional = projectService.queryProjectByUuId(componentUuid);
        if(!optional.isPresent()){
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST,"该项目不存在");
        }
        return ResponseDTO.ok();
    }
}
